package com.itlong.cloud.controller.settings;

import com.itlong.cloud.POJO.DTO.property.DeviceAiCameraInfoDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyDeviceAiCameraInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.equip.property.IPropertyAICameraServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

/**
 * <desc>
 *      ai摄像头设备管理控制器
 * </desc>
 *
 * @createDate2021/5/28
 **/
@RestController
@RequestMapping("equip/aiCamera")
@RefreshScope
public class PropertyAICameraController extends BaseController{

    @Autowired
    private IPropertyAICameraServiceClient iPropertyAICameraServiceClient;

    /**
     * <desc>
     *      添加AI摄像头
     * </desc>
     *
     * @param cameraInfoDTO 摄像头相关请求入参DTO
     * @return
     * @author Juguang.S
     * @createDate 2021/06/02
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "save", method = RequestMethod.POST)
    @LoggerInfo(operateDesc = LogDescConstant.AI_CAMERA_ADD_INFO_LOG_DESC)
    public Object save(DeviceAiCameraInfoDTO cameraInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            cameraInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceAiCameraInfoDTO.class, request);

            //入参校验
            if(StringUtils.isBlank(cameraInfoDTO.getDeviceName()) || StringUtils.isBlank(cameraInfoDTO.getDeviceIp()) ||
                    StringUtils.isBlank(cameraInfoDTO.getDeviceModel())||StringUtils.isBlank(cameraInfoDTO.getElevatorDeviceUnique())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            //使用正则表达式校验IP的格式是否正确
            String pattern= PlatformConstants.AI_CAMERA_IP_RULE;
            boolean isMatch= Pattern.matches(pattern, cameraInfoDTO.getDeviceIp());
            if(!isMatch){
                return new MessageVO(PropertyErrorCodeEnum.AI_CAMERA_IP_ERROR.getErrorCode());
            }

            //校验电梯是否已绑定其它摄像头 电梯与摄像头是一一对应关系
            if(iPropertyAICameraServiceClient.checkElevatorIsBind(cameraInfoDTO)>0){
                return new MessageVO(PropertyErrorCodeEnum.AI_CAMERA_BIND_ELEVATOR_ERROR.getErrorCode());
            }
            messageVO = new MessageVO(iPropertyAICameraServiceClient.save(cameraInfoDTO), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_设备管理_添加AI摄像头】添加AI摄像头失败", e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      修改AI摄像头
     * </desc>
     *
     * @param cameraInfoDTO 摄像头相关请求入参DTO
     * @return
     * @author Juguang.S
     * @createDate 2021/06/02
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "update", method = RequestMethod.POST)
    @LoggerInfo(operateDesc = LogDescConstant.AI_CAMERA_UPDATE_INFO_LOG_DESC)
    public Object update(DeviceAiCameraInfoDTO cameraInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            cameraInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceAiCameraInfoDTO.class, request);

            //入参校验
            if(StringUtils.isBlank(cameraInfoDTO.getDeviceId()) || StringUtils.isBlank(cameraInfoDTO.getDeviceName()) || StringUtils.isBlank(cameraInfoDTO.getDeviceIp()) ||
                    StringUtils.isBlank(cameraInfoDTO.getDeviceModel())||StringUtils.isBlank(cameraInfoDTO.getElevatorDeviceUnique())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            //使用正则表达式校验IP的格式是否正确
            String pattern= PlatformConstants.AI_CAMERA_IP_RULE;
            boolean isMatch= Pattern.matches(pattern, cameraInfoDTO.getDeviceIp());
            if(!isMatch){
                return new MessageVO(PropertyErrorCodeEnum.AI_CAMERA_IP_ERROR.getErrorCode());
            }

            //校验电梯是否已绑定其它摄像头 电梯与摄像头是一一对应关系
            if(iPropertyAICameraServiceClient.checkElevatorIsBind(cameraInfoDTO)>0){
                return new MessageVO(PropertyErrorCodeEnum.AI_CAMERA_BIND_ELEVATOR_ERROR.getErrorCode());
            }
            messageVO = new MessageVO(iPropertyAICameraServiceClient.update(cameraInfoDTO), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_设备管理_修改AI摄像头】修改AI摄像头失败", e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      根据设备ID获取AI摄像头详情
     * </desc>
     *
     * @param cameraInfoDTO 摄像头相关请求入参DTO
     * @return
     * @author Juguang.S
     * @createDate 2021/06/02
     */
    @ResponseBody
    @RequestMapping(path = "getAiCameraDetails", method = RequestMethod.POST)
    public Object getAiCameraDetails(DeviceAiCameraInfoDTO cameraInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            cameraInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceAiCameraInfoDTO.class, request);

            //入参校验
            if(StringUtils.isBlank(cameraInfoDTO.getDeviceId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyAICameraServiceClient.getAiCameraDetails(cameraInfoDTO));
        } catch (Exception e) {
            throw new DataAccessException("【物业_设备管理_修改AI摄像头】修改AI摄像头失败", e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      批量删除ai摄像头信息
     * </desc>
     *
     * @param cameraInfoDTO 摄像头相关请求入参DTO
     * @return
     * @author Juguang.S
     * @createDate 2021/06/02
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "batchRemove", method = RequestMethod.POST)
    @LoggerInfo(operateDesc = LogDescConstant.AI_CAMERA_BATCH_REMOVE_LOG_DESC)
    public Object batchRemove(DeviceAiCameraInfoDTO cameraInfoDTO) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            cameraInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceAiCameraInfoDTO.class, request);

            //入参校验
            if(StringUtils.isBlank(cameraInfoDTO.getDeviceIds())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyAICameraServiceClient.batchRemove(cameraInfoDTO);
        } catch (Exception e) {
            throw new DataAccessException("【物业_设备管理_批量删除ai摄像头信息】批量删除ai摄像头信息失败", e);
        }
        return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
    }

    /**
     * <desc>
     *  AI摄像头列表
     * </desc>
     *
     * @param cameraInfoDTO 摄像头相关请求入参DTO
     * @return 列表数据
     * @author Juguang.S
     * @createDate 2021/06/02
     */
    @ResponseBody
    @AuthAccessRequired
    @RequestMapping(path = "getPage", method = RequestMethod.POST)
    public Object getPage(DeviceAiCameraInfoDTO cameraInfoDTO) {
        MessageVO messageVO;
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            cameraInfoDTO = HttpProtocolUtil.parseRequestParamToDTO(DeviceAiCameraInfoDTO.class, request);
            Page<PropertyDeviceAiCameraInfoVO> page = iPropertyAICameraServiceClient.getAiCameraListByPage(cameraInfoDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        } catch (Exception e) {
            throw new DataAccessException("【物业_设备管理_AI摄像头list】获取分页列表失败", e);
        }
        return messageVO;
    }

}
